import { System } from 'cc';
import { sys } from 'cc';
import { Eventify } from 'cc';
import { _decorator, Component, Node } from 'cc';
import { wcm } from './wxScripts/WechatManager';
import { bdm } from './ttScripts/ByteDanceManager';
import { DevConfig } from '../managers/DevConfig';
import { ReactiveCommand } from '../libs/rxjs/cc3/ReactiveCommand';

const { ccclass, property } = _decorator;

@ccclass('SDKManager')
export class SDKManager extends Eventify(System) {

    public static instance: SDKManager;

    registerSystem() {
        switch (sys.platform) {
            case sys.Platform.BYTEDANCE_MINI_GAME:
                bdm.registerSystem();
                break;
            case sys.Platform.WECHAT_GAME:
                wcm.registerSystem();
                break;
            default:
                break;
        }
    }

    gameLogin() {
        switch (sys.platform) {
            case sys.Platform.BYTEDANCE_MINI_GAME:
                return bdm.gameLogin();
            case sys.Platform.WECHAT_GAME:
                return wcm.gameLogin();
            default:
                return new Promise(resolve => {
                    resolve(true);
                });
        }
    }

    report(eventName: string, data: Record<string, string | number>) {
        console.log('report', eventName, data);
        switch (sys.platform) {
            case sys.Platform.BYTEDANCE_MINI_GAME:
                bdm.report(eventName, data);
                break;
            case sys.Platform.WECHAT_GAME:
                wcm.report(eventName, data);
                break;
            default:
                break;
        }
    }

    getVersionText() {
        switch (sys.platform) {
            case sys.Platform.BYTEDANCE_MINI_GAME:
                let info = bdm.getEnvInfoSync();
                if (info != null) {
                    return `${info?.microapp?.mpVersion}-${info?.microapp?.envType}${DevConfig.hard ? '-hard' : ''}`;
                }
                break;
            default:
                break;
        }
        return `${DevConfig.hard ? '-hard' : ''}`;
    }

    onShareAppMessageCommand = ReactiveCommand.Create();

    shareAppMessage() {
        if (DevConfig.shareFakeProcess) {
            globalThis.GameManager.instance.frontTipShow('获得奖励').subscribe();
            setTimeout(() => {
                sdk.onShareAppMessageCommand.execute();
            }, 500);
        } else {
            switch (sys.platform) {
                case sys.Platform.BYTEDANCE_MINI_GAME:
                    bdm.shareAppMessage();
                    break;
                case sys.Platform.WECHAT_GAME:
                    wcm.shareAppMessage();
                    break;
                default:
                    break;
            }
        }
    }
}

globalThis.SDKManager = SDKManager;

export const sdk = SDKManager.instance = new SDKManager();